package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Gc;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Qyjg;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.criterion.*;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.ResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-28
 * Time: 上午10:07
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class GcDao extends SimpleHibernateTemplate<Gc,Long> {
    /**
     * 查询工程列表
     *
     * @param gcmc     工程名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Gc> findGcList(String gcmc,String qyjgName, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Gc> page = new Page<Gc>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (gcmc != null && !gcmc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" mc like ? ");
            param.add("%" + gcmc.trim() + "%");
        }
        if (qyjgName != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qyjg.qc like ? ");
            param.add("%" + qyjgName.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Gc";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Gc ";
        if (condition.length() != 0)
            hql = hql + " where " + condition +  "order by lastUpdated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List findGcList(Long qyjgId){
      Criteria critera=  this.getSession().createCriteria(this.entityClass)
                .createAlias("qyjg", "qyjg", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("mc").as("mc")))
                        .add(Restrictions.eq("qyjg.id", qyjgId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
      return critera.list();
    }


    public List<Gc> findAllGc() {
        Criteria critera=  this.getSession().createCriteria(this.entityClass)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("mc").as("mc")))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        return critera.list();
}

    /**
     * 判断岗位属性是否重复
     * @param id       岗位id
     * @param propertyValue       岗位属性值
     * @param propertyName       岗位属性名
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Gc where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Gc where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }
}
